/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Util;

import com.webaccess.retailws.Bean.MailBean;
import com.webaccess.retailws.Bean.PaymentBean;
import com.webaccess.retailws.Bean.UserInfoBean;
import com.webaccess.retailws.Delegate.MailDelegate;
import com.webaccess.retailws.Delegate.PaymentDelegate;

/**
 *
 * @author Owner
 */
public class PaymentUtil {

    PaymentDelegate payDelegate = null;
    PaymentBean payBean = null;
    MailBean mailInfo = null;
    CommonUtil commUtil = null;
    MailDelegate mailDelegateObj = null;
    SendMailUtil mailUtil = null;

    public Long savePaymentDelivery(String paymentType, Long userId) {
        payDelegate = new PaymentDelegate();
        Long paymentId = 0L;
        try {
            payBean = new PaymentBean();
            payBean.setPaymentId(paymentId);
            payBean.setPaymentType(paymentType);
            paymentId = payDelegate.savePaymentDelivery(payBean, userId);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return paymentId;
        }
    }

    public boolean savePaymentCard(Long orderId, Long cardTypeId, String cardNo, String cardHolderName, String cardExpiryMonth,
            String cardExpiryYear, Long bankId, Long cartId) {
        payDelegate = new PaymentDelegate();
        boolean isOrderUpdated = false;
        try {
            payBean = new PaymentBean();
            payBean.setOrderId(orderId);
            payBean.setBankId(bankId);
            payBean.setCardExpiryMonth(cardExpiryMonth);
            payBean.setCardExpiryYear(cardExpiryYear);
            payBean.setCardHolderName(cardHolderName);
            payBean.setCardTypeId(cardTypeId);
            payBean.setCardNo(cardNo);
            isOrderUpdated = payDelegate.savePaymentCard(payBean, orderId, cartId);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return isOrderUpdated;
        }
    }

    public boolean savePaymentBank(Long orderId, Long bankId, String bankAccountNo, Long cartId) {
        payDelegate = new PaymentDelegate();
        boolean isOrderUpdated = false;
        try {
            payBean = new PaymentBean();
            payBean.setOrderId(orderId);
            payBean.setBankAccountNo(bankAccountNo);
            payBean.setBankId(bankId);
            isOrderUpdated = payDelegate.savePaymentBank(payBean, orderId, cartId);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return isOrderUpdated;
        }
    }

    public void sendOrderMail(UserInfoBean userBean, PaymentBean payBean) {
        mailDelegateObj = new MailDelegate();
        mailUtil = new SendMailUtil();
        commUtil = new CommonUtil();
        try {
            StringBuffer messageBody = new StringBuffer();
            messageBody.append("E Shop - Order Shipped");
            messageBody.append("\n\n");
            messageBody.append("Hello ");
            messageBody.append(userBean.getFullName());
            messageBody.append(",");
            messageBody.append("\n\n");
            messageBody.append("Your products placed in the order has been shipped to you. ");
            messageBody.append("\n\n");
            messageBody.append("Order Details :-");
            messageBody.append("\n\n");
            messageBody.append("Order No:");
            messageBody.append(payBean.getOrderId());
            messageBody.append("\n");
            messageBody.append("Shipping Date:");
            messageBody.append(commUtil.formatDBDate(commUtil.getCurrentDateTime()));
            messageBody.append("\n");
            messageBody.append("Order Amount: Rs. ");
            messageBody.append(payBean.getPaydelTotal());
            messageBody.append("\n\n");
            messageBody.append("Thank you for Shopping with E Shop");
            mailInfo = new MailBean();
            mailInfo.setFromAddress("abhishek.agrawal@webaccess.co.in");
            mailInfo.setToAddress(userBean.getEmailId());
            mailInfo.setMsgSubject("E Shop - Payment Successfull");
            mailInfo.setMsgContent(messageBody.toString());
            mailInfo.setMsgDateTime(commUtil.getCurrentDateTime());
            mailInfo.setUserId(userBean.getUserId());
            mailInfo.setPurpose("Payment Successfull");

            Long mailId = mailDelegateObj.saveMailInfo(mailInfo);
            if (mailId > 0) {
                mailUtil.sendMail(mailInfo.getFromAddress(), mailInfo.getToAddress(), mailInfo.getMsgSubject(), mailInfo.getMsgContent());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
